package com.example.day14.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.day14.entity.Clazz;
import com.example.day14.entity.Student;
import com.example.day14.mapper.ClazzMapper;
import com.example.day14.service.ClazzService;
import com.example.day14.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author Linyh
 * @Description
 * @Date 2022/7/22 9:54
 **/

@RestController
@RequestMapping("/clazz")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private ClazzMapper clazzMapper;


    //查询所有班级信息和对应的学生信息
    @RequestMapping("/selectByCid")
    public List<Clazz> selectByCidStudent() {
        List<Clazz> clazzes = (List<Clazz>) clazzService.list();
        for (Clazz clazz : clazzes) {
            QueryWrapper<Student> wrapper = new QueryWrapper<>();
            wrapper.eq("cid",clazz.getCid());
            List<Student> students = studentService.list(wrapper);
            clazz.setStudents(students);
        }
        return clazzes;
    }


}
